﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using YAGMRC.Core.Models.GetGamesForPlayer;

namespace YAGMRC.Core.Commands
{
    public class GetGamesForPlayerCommand : ICommand, IResult<GetGamesForPlayerCommandResult>
    {
        #region Constructor

        private GetGamesForPlayerCommand()
        {
        }

        public GetGamesForPlayerCommand(Resolver resolver)
        {
            m_Resolver = resolver;
        }

        #endregion Constructor

        private Resolver m_Resolver;

        public void Execute()
        {
            if (!this.CanExecute())
            {
                this.Result = new GetGamesForPlayerCommandResult(new List<RootObject>());
                return;
            }

            AuthenticateCommandResult auth = m_Resolver.Get<AuthenticateCommandResult>();

            WebDownload executeWeb = m_Resolver.Get<WebDownload>();
            List<RootObject> game = executeWeb.Execute<List<RootObject>>("GetGamesForPlayer?authKey=" + auth.AuthID);

            if (null != game)
            {
                this.Result = new GetGamesForPlayerCommandResult(game);
            }
            else
            {
                this.Result = new GetGamesForPlayerCommandResult(new List<RootObject>());
            }
        }

        public bool CanExecute()
        {
            AuthenticateCommandResult auth = m_Resolver.Get<AuthenticateCommandResult>();
            if ((null != auth) && (auth.Authenticated))
            {
                return true;
            }

            return false;
        }

        public GetGamesForPlayerCommandResult Result
        {
            get;
            private set;
        }
    }
}